package com.baijiaoxi.common.utils.jdk;

import com.baijiaoxi.common.constant.DateTime;
import com.baijiaoxi.common.model.base.CoreException;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.baijiaoxi.common.model.base.CommonError.cannotParse;

public class BaseTypeParser {

    public static Integer parseToInteger(String str) {
        return parseToInteger(str, null);
    }

    public static Integer parseToInteger(String str, Integer nullDefaultValue) {
        if (str == null || str.isEmpty())
            return nullDefaultValue;
        return Integer.valueOf(str);
    }

    public static Integer parseToInteger(Number number) {
        return parseToInteger(number, null);
    }

    public static Integer parseToInteger(Number number, Integer nullDefaultValue) {
        if (number == null)
            return nullDefaultValue;
        return number.intValue();
    }

    public static Integer parseToInteger(Object object) {
        return parseToInteger(object, null);
    }

    public static Integer parseToInteger(Object object, Integer nullDefaultValue) {
        if (object == null)
            return nullDefaultValue;
        if (object instanceof Integer integer)
            return integer;
        if (object instanceof String string) {
            if (string.isEmpty())
                return nullDefaultValue;
            return Integer.valueOf(string);
        }
        if (object instanceof Number number)
            return number.intValue();
        throw CoreException.create(cannotParse, object.getClass(), "->Integer");
    }


    public static Long parseToLong(String str) {
        return parseToLong(str, null);
    }

    public static Long parseToLong(String str, Long nullDefaultValue) {
        if (str == null || str.isEmpty())
            return nullDefaultValue;
        return Long.valueOf(str);
    }

    public static Long parseToLong(Number number) {
        return parseToLong(number, null);
    }

    public static Long parseToLong(Number number, Long nullDefaultValue) {
        if (number == null)
            return nullDefaultValue;
        return number.longValue();
    }

    public static Long parseToLong(Object object) {
        return parseToLong(object, null);
    }

    public static Long parseToLong(Object object, Long nullDefaultValue) {
        if (object == null)
            return nullDefaultValue;
        if (object instanceof Long l)
            return l;
        if (object instanceof String string) {
            if (string.isEmpty())
                return nullDefaultValue;
            return Long.valueOf(string);
        }
        if (object instanceof Number number)
            return number.longValue();
        throw CoreException.create(cannotParse, object.getClass(), "->Long");
    }

    public static String parseDateToString(Object object, SimpleDateFormat simpleDateFormat) {
        return parseToString(object, simpleDateFormat, null);
    }

    public static String parseToString(Object object) {
        return parseToString(object, null, null);
    }

    public static String parseToString(Object object, String nullDefaultValue) {
        return parseToString(object, null, nullDefaultValue);
    }

    public static String parseToString(Object object, SimpleDateFormat simpleDateFormat, String nullDefaultValue) {
        if (object == null)
            return nullDefaultValue;
        if (object instanceof String string)
            return string;
        if (object instanceof Number number)
            return String.valueOf(number);
        if (object instanceof Date date)
            return simpleDateFormat == null ? DateTime.FULL_DATE.format(date) : simpleDateFormat.format(date);
        return object.toString();
    }

    public static Date parseToDate(Object object) {
        return parseToDate(object, null);
    }

    public static Date parseToDate(Object object, SimpleDateFormat simpleDateFormat) {
        if (object == null)
            return null;
        if (object instanceof Date date)
            return date;
        if (object instanceof String string) {
            if (simpleDateFormat == null)
                try {
                    return DateTime.FULL_DATE.parse(string);
                } catch (Exception e) {
                    try {
                        return DateTime.SHORT_DATE.parse(string);
                    } catch (Exception e1) {
                        throw CoreException.create(cannotParse, object, "->Date:长短格式皆失败");
                    }
                }
            else {
                try {
                    return simpleDateFormat.parse(string);
                } catch (Exception e) {
                    throw CoreException.create(cannotParse, object, "->Date:", simpleDateFormat.toPattern());
                }

            }
        }
        throw CoreException.create(cannotParse, object.getClass(), ":", object, "->Date:", simpleDateFormat == null ? "null" : simpleDateFormat.toPattern());
    }

    public static List<Long> parseToLongList(String[] strArr) {
        return Arrays.stream(strArr).map(BaseTypeParser::parseToLong).toList();
    }

    public static List<Long> splitToLongList(String str) {
        return splitToLongList(str, ",", 0);
    }

    public static List<Long> splitToLongList(String str, String regex) {
        return splitToLongList(str, regex, 0);
    }

    public static List<Long> splitToLongList(String str, String regex, int limit) {
        return parseToLongList(str.split(regex, limit));
    }

    public static List<Integer> parseToIntegerList(String[] strArr) {
        return Arrays.stream(strArr).map(BaseTypeParser::parseToInteger).toList();
    }

    public static List<Integer> splitToIntegerList(String str) {
        return splitToIntegerList(str, ",", 0);
    }

    public static List<Integer> splitToIntegerList(String str, String regex) {
        return splitToIntegerList(str, regex, 0);
    }

    public static List<Integer> splitToIntegerList(String str, String regex, int limit) {
        return parseToIntegerList(str.split(regex, limit));
    }
}
